package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 梁科
 * @Version 1.0
 * @ Date 2025/10/28 0:35
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;

    /**
     * 增加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 1. 判断该用户增加的菜品/套餐/口味是否在购物车中已经存在
        ShoppingCart list = shoppingCartMapper.list(shoppingCart);
        // 2. 已经存在，则只更新菜品/套餐的数量
        if (list != null) {
            shoppingCartMapper.updateNumberById(list.getId());
        } else {
            // 3. 不存在，则插入新的菜品/套餐记录
            if (shoppingCartDTO.getDishId() != null) {
                // 4. 传来的菜品id不为空，则增加购物车的是菜品
                ArrayList<Long> dishId = new ArrayList<>();
                dishId.add(shoppingCartDTO.getDishId());
                List<Dish> dishes = dishMapper.getById(dishId);
                shoppingCart.setAmount(dishes.get(0).getPrice());
                shoppingCart.setImage(dishes.get(0).getImage());
                shoppingCart.setName(dishes.get(0).getName());
            } else {
                // 5. 增加购物车的是套餐
                SetmealVO setmealVO = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setName(setmealVO.getName());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartMapper.insert(shoppingCart);
        }


    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart(Long userId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.show(userId);
        return shoppingCarts;
    }

    /**
     * 清空购物车
     *
     * @param userId
     */
    @Override
    public void cleanShoppingCart(Long userId) {
        shoppingCartMapper.cleanByUserId(userId);
    }

    /**
     * 删除购物车中的某一个菜品/套餐
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 1. 判断该项购物车中的数量是否>1,如果>1则只更新数量减1即可
        ShoppingCart list = shoppingCartMapper.list(shoppingCart);
        if (list.getNumber() > 1) {
            shoppingCartMapper.subNumber(list.getId());
        } else {
            // 2. 如果数量<=1,则直接删除该项购物车即可
            shoppingCartMapper.sub(shoppingCart);
        }
    }
}
